package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    //注入远程调用接口
    @Autowired
    private CustomerInfoFeignClient client;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String login(String code) {
        //1.拿着code进行远程调用，返回用户id
        Result<Long> loginResult = client.login(code);
        //2.判断如果返回失败了，返回错误提示
        Integer codeResult = loginResult.getCode();
        if(codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //3.获取返回的用户id
        Long customerId = loginResult.getData();
        //4.判断用户id是否为空,如果为空,返回错误提示
        if(customerId == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //5.生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //6.把用户id放到redis,设置过期时间
        // redisTemplate.opsForValue().set(token,customerId.toString(),30, TimeUnit.MINUTES);
        stringRedisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                customerId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS);
        //7.返回token
        return token;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo() {
        //从ThreadLocal中获取用户id
        Long customerId = AuthContextHolder.getUserId();
        //远程调用获取用户登录信息
        Result<CustomerLoginVo> customerLoginVoResult = client.getCustomerLoginInfo(Long.valueOf(customerId));
        if(customerLoginVoResult.getCode() != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        //返回用户登录信息
        return customerLoginVo;
    }

    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        Result<Boolean> result = client.updateWxPhoneNumber(updateWxPhoneForm);
        if(result.getCode() != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return true;
    }
}
